//
//  LZTopicCell.m
//  LZHundredJie
//
//  Created by apple on 16/4/13.
//  Copyright © 2016年 m14a.cn. All rights reserved.
//

#import "LZTopicCell.h"
#import "LZTopic.h"
#import "UIImageView+WebCache.h"

#import "LZTopicPictureView.h"
#import "LZTopicVideoView.h"
#import "LZTopicVoiceView.h"

@interface LZTopicCell ()

// 控件的命名 = 功能作用 + 控件类型
@property (nonatomic, weak) IBOutlet UIImageView *profileImageView;

@property (nonatomic, weak) IBOutlet UILabel *nameLabel;

@property (nonatomic, weak) IBOutlet UILabel *passtimeLabel;

@property (nonatomic, weak) IBOutlet UILabel *text_label;

@property (nonatomic, weak) IBOutlet UIButton *dingButton;

@property (nonatomic, weak) IBOutlet UIButton *caiButton;

@property (nonatomic, weak) IBOutlet UIButton *repostButton;

@property (nonatomic, weak) IBOutlet UIButton *commentButton;

@property (nonatomic, weak) IBOutlet UIView *topCmtView;

@property (nonatomic, weak) IBOutlet UILabel *topCmtLabel;

/* 中间控件 */
/** 图片控件 */
@property (nonatomic, weak) LZTopicPictureView *pictureView;
/** 声音控件 */
@property (nonatomic, weak) LZTopicVoiceView *voiceView;
/** 视频控件 */
@property (nonatomic, weak) LZTopicVideoView *videoView;

@end

@implementation LZTopicCell

- (LZTopicPictureView *)pictureView
{
    if (_pictureView == nil) {
        LZTopicPictureView *pictureView = [LZTopicPictureView lz_viewFromXib];
        _pictureView = pictureView;
        [self.contentView addSubview:pictureView];
    }
    return _pictureView;
}

- (LZTopicVideoView *)videoView
{
    if (_videoView == nil) {
        LZTopicVideoView *videoView = [LZTopicVideoView lz_viewFromXib];
        _videoView = videoView;
        [self.contentView addSubview:videoView];
    }
    return _videoView;
}

- (LZTopicVoiceView *)voiceView
{
    if (_voiceView == nil) {
        LZTopicVoiceView *voiceView = [LZTopicVoiceView lz_viewFromXib];
        _voiceView = voiceView;
        [self.contentView addSubview:voiceView];
    }
    return _voiceView;
}

- (void)awakeFromNib
{
    
    // 设置背景
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
}

- (void)setTopic:(LZTopic *)topic
{
    _topic = topic;
    
    // 设置头像
    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:topic.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        if (image == nil)   return ;
        
        // 获取圆形图片
        UIImage *newImage = [image circleImage];
        self.profileImageView.image = newImage;
        
        }];
    
    // 设置昵称
    self.nameLabel.text = topic.name;
    // 设置时间
    self.passtimeLabel.text = topic.passtime;
    // 设置正文
    switch (topic.type) {
        case LZTopicTypeWord: { // 段子
            self.pictureView.hidden = YES;
            self.videoView.hidden = YES;
            self.voiceView.hidden = YES;
            break;
        }
        case LZTopicTypeVoice: { // 声音
            self.voiceView.hidden = NO;
            
            self.voiceView.topic = topic;
            
            self.videoView.hidden = YES;
            self.pictureView.hidden = YES;
            break;
        }
        case LZTopicTypeVideo: { // 视频
            self.videoView.hidden = NO;
            self.videoView.topic = topic;
            self.pictureView.hidden = YES;
            self.voiceView.hidden = YES;
            break;
        }
        case LZTopicTypePicture: { // 图片
            self.pictureView.hidden = NO;
            self.pictureView.topic = topic;
            self.voiceView.hidden = YES;
            self.videoView.hidden = YES;
            break;
        }
    }
    
    // 最热评论
    if (topic.top_cmt.count) { // 有最热评论
        
        self.topCmtView.hidden = NO;
        // 拿到用户名
        NSString *userName = topic.top_cmt.firstObject[@"user"][@"username"];
        NSString *content = topic.top_cmt.firstObject[@"content"];
        if (content.length == 0) { // 没有文字内容，是个语音评论
            content = @"[语音评论]";
        }
        // 拿到评论
        self.topCmtLabel.text = [NSString stringWithFormat:@"%@ : %@", userName, content];
        
    } else {
        self.topCmtView.hidden = YES;
    }
    
    self.text_label.text = topic.text;
    
    // 设置按钮
    [self setupButton:self.dingButton number:topic.ding placeholder:@"顶"];
    [self setupButton:self.caiButton number:topic.cai placeholder:@"踩"];
    [self setupButton:self.repostButton number:topic.repost placeholder:@"分享"];
    [self setupButton:self.commentButton number:topic.comment placeholder:@"评论"];
}

// 布局子控件
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    switch (self.topic.type) {
        case LZTopicTypePicture:    // 图片
            self.pictureView.frame = self.topic.middleF;
            break;
            
        case LZTopicTypeVideo:      // 视频
            self.videoView.frame = self.topic.middleF;
            break;
            
        case LZTopicTypeVoice:      // 声音
            self.voiceView.frame = self.topic.middleF;
            break;
    }
}

- (void)setFrame:(CGRect)frame
{
    frame.size.height -= LZMargin;
    [super setFrame:frame];
}

- (void)setupButton:(UIButton *)button number:(NSInteger)number placeholder:(NSString *)placeholder
{
    if (number > 10000) {
        [button setTitle:[NSString stringWithFormat:@"%.1f万", number / 10000.0] forState:UIControlStateNormal];
    } else if (number > 0) {
        [button setTitle:[NSString stringWithFormat:@"%zd", number] forState:UIControlStateNormal];
    } else {
        [button setTitle:placeholder forState:UIControlStateNormal];
    }
}

@end
